package com.lagou.rabbitmq.demo.homework.listener;

import com.lagou.rabbitmq.demo.homework.service.RedisService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.lagou.rabbitmq.demo.homework.config.RabbitConfig.ORDER_DLX_QUEUE_NAME;

/**
 * @Author: gule
 * @Date: 2020/10/16
 */
@Component
public class OrderCancelListener implements ChannelAwareMessageListener {

    @Autowired
    private RedisService redisService;

    @Override
    @RabbitListener(queues = "order_dlx_queue.dlx", ackMode = "MANUAL")
    public void onMessage(Message message, Channel channel) throws IOException {
        String orderId = new String(message.getBody());
        System.out.println("订单超时,取消订单：" + orderId);
        redisService.canceledPayOrder(orderId);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
